package com.changgou.order.service.impl;

import com.changgou.entity.Result;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.order.pojo.OrderItem;
import com.changgou.order.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CartServiceImpl implements CartService {

    private static final String CART="Cart_";

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SkuFeign skuFeign;

    @Autowired
    private SpuFeign spuFeign;

    /**
     * 从商品详情页面添加购物车
     * @param skuId
     * @param num(负数为减)
     * @param username
     */
    @Override
    public void add(String skuId, Integer num, String username) {

        /*
        * 1.查询redis中的数据
        * 2.如果redis已有数据，则修改
        * 3.如果redis没有数据，则添加
        * */

        //从redis中以CART+username为key,skuid为条件查找购物车有没有这项商品
        //规格不同skuid也不同
        OrderItem orderItem = (OrderItem) redisTemplate.boundHashOps(CART + username).get(skuId);

        if (orderItem != null){
            //查到了，存在该商品,则修改信息
            //修改购买数量，num可为负数
            orderItem.setNum(orderItem.getNum() + num);
            if(orderItem.getNum() <= 0){
                //购买数量小于等于0则删除
                redisTemplate.boundHashOps(CART+username).delete(skuId);
                return;
            }
            //修改总价
            orderItem.setMoney(orderItem.getNum()*orderItem.getPrice());
            //修改实付金额
            orderItem.setPayMoney(orderItem.getNum()*orderItem.getPrice());


        }else {
            //没查到，redis中没该商品，则添加
            Result<Sku> byId = skuFeign.findById(skuId);
            Sku sku = skuFeign.findById(skuId).getData();
            Spu spu = spuFeign.findSpuById(sku.getSpuId()).getData();

            //将sku结合spu转换成orderItem，此时orderItem为null
            orderItem = this.sku2OrderItem(sku,spu,num);
        }

        //存入redis
        redisTemplate.boundHashOps(CART+username).put(skuId,orderItem);

    }

    /**
     * 查询购物车列表
     * @param username
     * @return
     */
    @Override
    public Map list(String username) {

        Map map = new HashMap<>();

        List<OrderItem> orderItemList = redisTemplate.boundHashOps(CART + username).values();

        //购物车商品sku数据
        map.put("orderItemList",orderItemList);
        //商品数量和总价格
        Integer totalNum = 0;
        Integer totalPrice = 0;

        for (OrderItem orderItem : orderItemList) {
            totalNum += orderItem.getNum();
            totalPrice += orderItem.getMoney();
        }

        map.put("totalNum",totalNum);
        map.put("totalPrice",totalPrice);

        return map;
    }

    /**
     * 将sku结合spu封装成orderItem
     * @param sku
     * @param spu
     * @param num
     * @return
     */
    private OrderItem sku2OrderItem(Sku sku, Spu spu, Integer num) {

        OrderItem orderItem = new OrderItem();
        orderItem.setSpuId(sku.getSpuId());
        orderItem.setSkuId(sku.getId());
        orderItem.setName(sku.getName());
        orderItem.setPrice(sku.getPrice());
        orderItem.setNum(num);
        orderItem.setMoney(num*orderItem.getPrice());//总价
        orderItem.setPayMoney(num*orderItem.getPrice());//实付金额
        orderItem.setImage(sku.getImage());
        orderItem.setWeight(sku.getWeight()*num);//总重量

        //分类id
        orderItem.setCategoryId1(spu.getCategory1Id());
        orderItem.setCategoryId2(spu.getCategory2Id());
        orderItem.setCategoryId3(spu.getCategory3Id());

        return orderItem;

    }
}
